/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.table.cellRenderer;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.table.cellRenderer
 * @date 22/04/2014 - 14:13:20
 */
public class DefaultRenderer extends DefaultTableCellRenderer {
	
	private static final long serialVersionUID = 3951957173726790570L;
	
	/**
	 * 
	 */
	public DefaultRenderer() {
	
	}
	
	/**
	 * @param alignment
	 *        One of the following constants
	 *        defined in <code>SwingConstants</code>: <code>LEFT</code>, <code>CENTER</code> (the default for image-only labels), <code>RIGHT</code>, <code>LEADING</code> (the default for text-only labels) or <code>TRAILING</code>.
	 */
	public DefaultRenderer(final int alignment) {
	
		setHorizontalAlignment(alignment);
	}
	
	@Override
	public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
	
		//
		if(isSelected) {
			
			setForeground(table.getSelectionForeground());
			setBackground(table.getSelectionBackground());
			
		}else {
			
			setForeground(table.getForeground());
			setBackground(table.getBackground());
		}
		
		setFont(table.getFont());
		
		setText(formatValue(value));
		
		table.setRowHeight(25);
		
		return this;
	}
	
	/**
	 * @param value
	 * @return formated value or empty String
	 */
	public String formatValue(final Object value) {
	
		return value != null ? value.toString() : "";
	}
}
